package com.burgeon.backend.SHOP.service.Impl;

import com.burgeon.backend.SHOP.entity.Basket;
import com.burgeon.backend.SHOP.entity.Goods;
import com.burgeon.backend.SHOP.mappers.BasketMapper;
import com.burgeon.backend.SHOP.mappers.GoodsMapper;
import com.burgeon.backend.SHOP.service.AppActivityService;
import com.burgeon.backend.SHOP.service.BasketService;
import com.burgeon.backend.SHOP.service.util.MathUtil;
import com.burgeon.backend.service.ex.InsertException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BasketServiceImpl implements BasketService {
    private final static Log logger = LogFactory.getLog(BasketService.class);

    @Autowired
    private BasketMapper basketMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private AppActivityService appActivityService;

    @Override
    public String getBasketCount(String uid, String token) {
        String s = basketMapper.basketCount(uid, token);
        if (null == s) {
            s = "0";
        }
        return s;
    }

    @Override
    public List<Goods> GetPayGoodsData(String uid, String token,String discount,String isPlus) {
        List<Basket> baskets = basketMapper.thisVipSelectedData(uid, token);

        // 遍历并计算总价：
        ArrayList<Goods> data = new ArrayList<>();
        for (Basket basket : baskets) {
            Goods goods = goodsMapper.getGoods(basket.getGid());
            //判断plus会员
            if (!"Y".equals(isPlus)){
                goods.setPlusPrice(null);
            }

            //计算会员价
            if ("Y".equals(goods.getIsVipPrice())) {
                goods.setVipPrice(String.valueOf(MathUtil.multiply(new BigDecimal(goods.getOriginal()), new BigDecimal(discount), 0)));
            }
            goods.setLowPrice(minPrice(goods.getPrice(), goods.getVipPrice(), goods.getPlusPrice()));

            goods.setNum(basket.getNum());
            goods.setGoodsType(basket.getType());
            data.add(goods);
        }
        return data;
    }

    @Override
    public void DelBasket(String id) {
        try {
            basketMapper.delData(id);
        } catch (Exception e) {
            System.err.println("删除错误");
        }
    }

    @Override
    public void SetBasketData(String id, String num, String selected, String token) {
        basketMapper.setData(id, num, selected, token);
    }

    @Override
    public List<Basket> GetBasket(String uid, String token, String discount,String isPlus) {
        try {
            List<Basket> data = basketMapper.getData(uid, token);
            for (Basket basket : data) {
                Goods goods = goodsMapper.getGoods(basket.getGid());



                if (goods != null && !"N".equals(goods.getIsPutaway())) {
                    //判断plus会员
                    if (!"Y".equals(isPlus)){
                        goods.setPlusPrice(null);
                    }

                    //计算会员价
                    if ("Y".equals(goods.getIsVipPrice())) {
                        goods.setVipPrice(String.valueOf(MathUtil.multiply(new BigDecimal(goods.getOriginal()), new BigDecimal(discount), 0)));
                    }


                    goods.setPrice(minPrice(goods.getPrice(), goods.getVipPrice(), goods.getPlusPrice()));
                    if (null != goods.getActivityId()) {
                        goods.setActivitieitem(appActivityService.getActvityById(goods.getActivityId()));
                    }
                    basket.setGoods(goods);
                }
            }
            return data;
        } catch (Exception e) {
            logger.error(e.getMessage());
            System.err.println(e.getMessage());
            return null;
        }
    }


    private String minPrice(String value1, String value2, String value3) {


        int a = Integer.parseInt(value1 == null ? "2147483647" : value1);
        int b = Integer.parseInt(value2 == null ? "2147483647" : value2);
        int c = Integer.parseInt(value3 == null ? "2147483647" : value3);
        int n = a < b ? a : b; //找出a和b最大值。
        int m = n < c ? n : c;//用n和c比较。
        return String.valueOf(m);
    }

    @Override
    public void addBasket(String uid, String gid, String type, String code, String token) throws InsertException {
        Basket baskett = basketMapper.findBasket(code, uid, gid, token);
        if (null != baskett) {
            //存在数量加1
            SetBasketData(baskett.getId(), String.valueOf(Integer.parseInt(baskett.getNum()) + 1), baskett.getSelected(), token);
            return;
        }
        // 创建当前时间对象
        try {
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String now = sdf.format(date);
            //Goods goods = goodsMapper.selectBygid(gid);
            Basket basket = new Basket();
            basket.setGid(gid);
            basket.setCode(code);
            basket.setNum("1");//默认
            basket.setSelected("Y");//默认
            basket.setUid(uid);
            basket.setType(type);
            basket.setAccount(token);
            basket.setToken(token);
            basket.setCreatetime(now);
            basket.setModtime(now);
            basketMapper.insertBasket(basket);
        } catch (Exception e) {
            e.printStackTrace();
            throw new InsertException("添加购物车出现错误");
        }

    }


}
